package dbTest.Util;

import java.sql.*;

/**
 * @Author: Tiangle
 * @Date: 2021/9/27 11:26
 */
public class PrepareStatementUtil {
    static PreparedStatement ps = null;
    public static Object excute(String sql,Object...args) {
        Connection con = DBUtil.getConnection();
        String sl = sql.toLowerCase();
        try {
            ps = con.prepareStatement(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        int i = 1;
        for (Object o:args
        ) {
            try {
                ps.setObject(i,o);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            i++;
        }
        if (!sl.trim().startsWith("select")){
            int l= 0;
            try {
                l = ps.executeUpdate();
            } catch (SQLException e) {
                e.printStackTrace();
            }


            return l;
        }else{
            ResultSet rs = null;
            try {
                rs = ps.executeQuery();
            } catch (SQLException e) {
                e.printStackTrace();
            }

            return rs;
        }

    }
    public static void close(){
        try {
            ps.close();
            ps=null;
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
